package com.throttle.raildroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InitActivity extends Activity {
	private Throttle throttle;
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.init);
    	final Button button = (Button) findViewById(R.id.ok);
    	button.setOnClickListener(new OnClickListener() {
			//default addr for android emulator to train emulator connection should be 10.0.2.2
    		//default port for the train emulator is 14804
			public void onClick(View v) {
				EditText addr = (EditText) findViewById(R.id.address);
				EditText port = (EditText) findViewById(R.id.port);
				int portNum = 0;
				try{
					portNum = Integer.parseInt(port.getText().toString());
				}catch(Exception e){
					Toast.makeText(InitActivity.this, "Please enter a valid port number.", Toast.LENGTH_SHORT).show();
					return;
				}
				String ipAddr = addr.getText().toString();
				boolean connected = StaticLogList.throttle.connect(ipAddr,portNum);
				if (!connected){
					StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Failed to connect to rail server @ "+ipAddr+":"+port.getText().toString()));
					Toast.makeText(InitActivity.this, "Connection Failed", Toast.LENGTH_SHORT).show();
				}else{
					StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Throttle connected to rail server @ "+ipAddr+":"+port.getText().toString()));
					Toast.makeText(InitActivity.this, "Throttle Connected", Toast.LENGTH_SHORT).show();
				}
			}
		});
    } 
}